/*jslint browser: true, cap: false, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function() {

    //Module-level members:
    var data = SONGHAY.ui.DataValidation;

    SONGHAY.ui.GenericWebEditor.xmlHelper =
    {
        getDocumentSegment: function(oInput, oDocId) {
            var vElementString = '';
            var vSegmentId = null;

            if (oInput && (oInput.id) && oInput.checked) {
                vSegmentId = oInput.id;
                if (vSegmentId) { vSegmentId = vSegmentId.replace('Segment', ''); }

                if (oDocId && (oDocId.id) && (oDocId.value) && (oDocId.value.length > 0)) {
                    vElementString = '<SegmentDocument><SegmentId>' + vSegmentId + '</SegmentId><DocumentId>' + oDocId.value + '</DocumentId></SegmentDocument>';
                }
                else {
                    vElementString = '<SegmentDocument><SegmentId>' + vSegmentId + '</SegmentId><DocumentId /></SegmentDocument>';
                }
            }

            return vElementString;
        },

        getDocumentXmlString: function(oElement) {
            var vXmlString = ''; var o = null; var i = 0;

            switch (oElement.id) {
                case 'DeleteDocument':
                    return '<FormData><DocumentId>' + oElement.form.DocumentId.value + '</DocumentId></FormData>';

                case 'SaveDocument':
                case 'SaveNewDocument':
                    if (data.isNotComplete(oElement.form.Title, 'Document Title')) { return 'false'; }
                    if (data.isNotComplete(oElement.form.CreateDateText, 'Publication Date')) { return 'false'; }

                    vXmlString = '<FormData>\n';

                    if (oElement.form.SegmentId) { vXmlString += data.getXmlElement(null, data.getRadioValue(oElement.form.SegmentId), true, 'SegmentId'); }
                    vXmlString += data.getXmlElement(oElement.form.DocumentId);
                    vXmlString += data.getXmlElement(oElement.form.ClientId, null, true, 'ClientId');
                    vXmlString += data.getXmlElement(oElement.form.CreateDateText, null, true, 'CreateDate');
                    vXmlString += data.getXmlElement(oElement.form.ModificationDateText, null, true, 'ModificationDate');
                    vXmlString += data.getXmlElement(oElement.form.DocumentShortName);
                    vXmlString += data.getXmlElement(oElement.form.FileName);
                    vXmlString += data.getXmlElement(oElement.form.IsActive);
                    vXmlString += data.getXmlElement(oElement.form.IsRoot, null, true, 'IsRoot');
                    vXmlString += data.getXmlElement(oElement.form.Path);
                    vXmlString += data.getXmlElement(oElement.form.SortOrdinal);
                    vXmlString += data.getXmlElement(oElement.form.TemplateId, null, true, 'TemplateId');
                    vXmlString += data.getXmlElement(oElement.form.Tag);
                    vXmlString += data.getXmlElement(oElement.form.Title);

                    vXmlString += '<DocumentSegments>';
                    o = YAHOO.util.Dom.getElementsByClassName('CheckBox', 'input', 'DocumentSegments');
                    if (o && o.length) { for (i = 0; i < o.length; i++) { vXmlString += self.getDocumentSegment(o[i], oElement.form.DocumentId); } }
                    vXmlString += '</DocumentSegments>';

                    vXmlString += '<DocumentKeywords>';
                    o = oElement.form.DocumentKeywords;
                    if (o) {
                        o = o.value.split(',');
                        for (i = 0; i < o.length; i++) {
                            vXmlString += '<WebKeyword>';
                            vXmlString += data.getXmlElement(oElement.form.DocumentId);
                            vXmlString += '<KeywordValue><![CDATA[' + o[i] + ']]></KeywordValue>';
                            vXmlString += '</WebKeyword>';
                        }
                    }
                    vXmlString += '</DocumentKeywords>';


                    vXmlString += '</FormData>\n';

                    return vXmlString;
            }
        },

        getFragmentXmlString: function(oElement) {
            switch (oElement.id) {
                case 'DeleteFragment':
                    return '<FormData><FragmentId>' + oElement.form.FragmentId.value + '</FragmentId></FormData>';

                case 'SaveFragment':
                case 'SaveNewFragment':
                    if (data.isNotComplete(oElement.form.CreateDateText, 'Publication Date')) { return 'false'; }

                    var vXmlString = '<FormData>\n';

                    vXmlString += data.getXmlElement(oElement.form.FragmentId);
                    vXmlString += data.getXmlElement(oElement.form.ClientId);
                    vXmlString += data.getXmlElement(oElement.form.CreateDateText, null, true, 'CreateDate');
                    vXmlString += data.getXmlElement(oElement.form.EndDateText, null, true, 'EndDate');
                    vXmlString += data.getXmlElement(oElement.form.ModificationDateText, null, true, 'ModificationDate');
                    vXmlString += data.getXmlElement(oElement.form.DocumentId, '<DocumentId />');
                    vXmlString += data.getXmlElement(oElement.form.NextFragmentId, '<NextFragmentId />');
                    vXmlString += data.getXmlElement(oElement.form.PrevFragmentId, '<PrevFragmentId />');
                    vXmlString += data.getXmlElement(oElement.form.FragmentDisplayName);
                    vXmlString += data.getXmlElement(oElement.form.FragmentName);

                    var oProseBlock = YAHOO.util.Dom.get('ProseFragment');

                    if (oProseBlock && oProseBlock.value.length > 1024) {
                        vXmlString += '<ItemChar />';
                        vXmlString += '<ItemText xml:space="preserve"><![CDATA[' + data.formatXmlStringInput(oProseBlock.value) + ']]></ItemText>\n';
                    }
                    else if (oProseBlock) {
                        vXmlString += '<ItemChar xml:space="preserve"><![CDATA[' + data.formatXmlStringInput(oProseBlock.value) + ']]></ItemChar>\n';
                        vXmlString += '<ItemText />';
                    }

                    vXmlString += data.getXmlElement(oElement.form.IsActive);
                    vXmlString += data.getXmlElement(oElement.form.IsNext);
                    vXmlString += data.getXmlElement(oElement.form.IsPrevious);
                    vXmlString += data.getXmlElement(oElement.form.IsWrapper);
                    vXmlString += data.getXmlElement(oElement.form.SortOrdinal);

                    vXmlString += '</FormData>\n';

                    return vXmlString;
            }
        },

        getSegmentXmlString: function(oElement) {
            var vXmlString = '';

            switch (oElement.id) {
                case 'DeleteSegment':
                    return '<FormData><SegmentId>' + oElement.form.SegmentId.value + '</SegmentId></FormData>';

                case 'SaveSegment':
                case 'SaveNewSegment':
                    if (data.isNotComplete(oElement.form.SegmentName, 'Segment Name')) { return 'false'; }
                    if (data.isNotComplete(oElement.form.CreateDateText, 'Publication Date')) { return 'false'; }

                    vXmlString = '<FormData>\n';

                    vXmlString += data.getXmlElement(oElement.form.SegmentId);
                    vXmlString += data.getXmlElement(oElement.form.ClientId, null, true, 'ClientId');
                    vXmlString += data.getXmlElement(oElement.form.CreateDateText, null, true, 'CreateDate');
                    vXmlString += data.getXmlElement(oElement.form.IsActive);
                    vXmlString += data.getXmlElement(oElement.form.ParentSegmentId);
                    vXmlString += data.getXmlElement(oElement.form.SegmentName);
                    vXmlString += data.getXmlElement(oElement.form.SortOrdinal);

                    vXmlString += '</FormData>\n';

                    return vXmlString;
            }
        },

        getSearchXmlString: function(oElement) {
            var o = null;
            var oForm = (oElement.form) ? oElement.form : null;

            if (oForm && (oForm.id == 'SearchForm')) {
                o = YAHOO.util.Dom.get('SearchText');
                if (data.isNotComplete(o, 'Search Text')) { return 'false'; }

                if (o && o.value && (o.value == 'Search...')) {
                    o.value = '';
                    if (data.isNotComplete(o, 'Search Text')) { return 'false'; }
                }
            }

            var vXmlString = '<FormData>\n';
            vXmlString += '<SearchText><![CDATA[' + oElement.form.SearchText.value + ']]></SearchText>\n';
            vXmlString += '</FormData>\n';

            return vXmlString;
        }
    };

    //Module-level "this":
    var self = SONGHAY.ui.GenericWebEditor.xmlHelper;

})();
